const util = require('../../utils/util.js')
// const API = require('../../utils/api.js')  // 注释掉API引用
const app = getApp()
// 注释掉地图SDK，使用静态数据
// const QQMapWX = require('../../utils/qqmap-wx-jssdk.js')
// var qqmapsdk

Page({
  data: {
    isLogin: true, // 默认已登录状态
    color: "#83AF9B", // 默认颜色
    avatarUrl: "/images/person_selected.png", // 默认头像
    username: "测试用户",
    
    // 最近活动数据
    recentActivities: [
      {
        id: 1,
        type: 'print',
        title: '打印文档 合同文件.pdf',
        time: '2分钟前'
      },
      {
        id: 2,
        type: 'upload',
        title: '上传文件 财务报表.xlsx',
        time: '15分钟前'
      },
      {
        id: 3,
        type: 'collaborate',
        title: '团队协作 项目计划.docx',
        time: '1小时前'
      }
    ],
    
    userServiceList: [
      {
        icon: "/images/userinfo.png",
        text: "我的信息"
      },
      {
        icon: "/images/editPassword.png",
        text: "修改密码"
      },
      {
        icon: "/images/exit_login.png",
        text: "退出登录"
      }
    ],
    optionalFunction: {
      icon: "/images/record.png",
      text: "查询历史"
    },
    showLocation: true,
    location: {
      address: "北京市海淀区中关村南大街5号",
      latitude: 39.9761,
      longitude: 116.3282
    }
  },

  // 添加新的页面跳转函数
  goToProfile: function() {
    wx.navigateTo({
      url: '/packageA/updateInfo/updateInfo'
    })
  },
  
  goToFiles: function() {
    wx.navigateTo({
      url: '/pages/fileList/fileList'
    })
  },
  
  goToTeam: function() {
    wx.showToast({
      title: '团队功能暂未开放',
      icon: 'none'
    })
  },
  
  goToPrint: function() {
    wx.navigateTo({
      url: '/packageB/printService/printService'
    })
  },
  
  goToSettings: function() {
    wx.navigateTo({
      url: '/packageA/settings/settings'
    })
  },
  
  goToHelp: function() {
    wx.navigateTo({
      url: '/packageA/help/help'
    })
  },
  
  changePassword: function() {
    wx.navigateTo({
      url: '/packageA/editPassword/editPassword'
    })
  },
  
  viewAllActivities: function() {
    wx.navigateTo({
      url: '/pages/activityList/activityList'
    })
  },
  
  editProfile: function() {
    this.goToProfile();
  },
  
  logout: function() {
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: function(res) {
        if (res.confirm) {
          // 清除用户信息
          app.globalData.userinfo = null;
          app.globalData.usertype = "";
          app.globalData.isLogin = false;
          
          // 清除本地存储
          wx.removeStorageSync('userinfo');
          wx.removeStorageSync('usertype');
          wx.setStorageSync('isLogin', false);
          
          // 跳转到登录页面
          wx.reLaunch({
            url: '/packageA/login/login'
          });
        }
      }
    });
  },

  onLoad: function () {
    // 注释掉地图SDK初始化
    // qqmapsdk = new QQMapWX({
    //   key: "57EBZ-7NMWS-GQ5O7-6VZHB-EZ3FQ-QNFCF"
    // })
    
    // 直接初始化静态数据
    this.initStaticData();
  },
  
  // 添加初始化静态数据的方法
  initStaticData: function() {
    // 模拟用户数据
    app.globalData.isLogin = true;
    app.globalData.usertype = "customer";
    app.globalData.userinfo = {
      id: "user123",
      name: "测试用户",
      avatarUrl: "/images/default_user_avatar.png",
      tel: "13800138000"
    };
    
    // 更新页面数据
    this.setData({
      isLogin: true,
      color: "#83AF9B",
      avatarUrl: "/images/default_user_avatar.png",
      username: "测试用户"
    });
  },

  onShow: function() {
    // 直接使用静态数据，不依赖全局状态
    this.setData({
      isLogin: true
    });
    this.setTopBanner();
    this.setBottomBanner();
  },
  
  setTopBanner: function() {
    // 使用静态数据，不进行API调用
    let type = "customer";  // 可以根据需要更改类型
    
    // 根据类型设置不同的颜色和是否显示位置
    if(type == "producer") {
      this.setData({
        color: "#FC9D9A",
        showLocation: true
      });
    } else if(type == "transporter") {
      this.setData({
        color: "#F9CDAD",
        showLocation: true
      });
    } else if(type == "seller") {
      this.setData({
        color: "#C8C8A9",
        showLocation: true
      });
    } else if(type == "customer") {
      this.setData({
        color: "#83AF9B",
        showLocation: false
      });
    }
    
    // 设置用户名和头像
    this.setData({
      username: "测试用户",
      avatarUrl: "/images/person_selected.png"
    });
    
    // 注释掉位置获取相关代码
    /*
    if(type != "customer") {
      wx.getSetting({
        success(res) {
          if(res.authSetting['scope.userLocation'] == null) {
            wx.authorize({
              scope: 'scope.userLocation',
              success(){
                that.getLocation()
              }
            })
          } else if(res.authSetting['scope.userLocation'] == true && that.data.location.address == "暂无定位信息") {
            that.getLocation()
          }
        }
      })
    }
    */
  },
  
  setBottomBanner: function() {
    // 使用静态数据设置底部功能按钮
    let type = "customer"; // 可以根据需要更改类型
    let optionalFunction;
    
    if(type == "producer") {
      optionalFunction = {
        icon: "/images/code.png",
        text: "生成产品编码"
      };
    } else if(type == "transporter") {
      optionalFunction = {
        icon: "/images/location.png",
        text: "记录物流信息"
      };
    } else if(type == "seller") {
      optionalFunction = {
        icon: "/images/icon_seller_jiaoyi.png",
        text: "记录销售信息"
      };
    } else if(type == "customer") {
      optionalFunction = {
        icon: "/images/record.png",
        text: "查询历史"
      };
    }
    
    this.setData({
      optionalFunction: optionalFunction
    });
  },
  
  producerLogin: function() {
    wx.navigateTo({
      url: '/pages/login/login?type=producer',
    });
  },
  
  transporterLogin: function() {
    wx.navigateTo({
      url: '/pages/login/login?type=transporter',
    });
  },
  
  sellerLogin: function() {
    wx.navigateTo({
      url: '/pages/login/login?type=seller',
    });
  },
  
  customerLogin: function() {
    wx.navigateTo({
      url: '/pages/login/login?type=customer',
    });
  },
  
  tapAvatar: function() {
    // 注释掉上传头像的API调用，改为简单的提示
    wx.showToast({
      title: '静态演示模式，无法上传头像',
      icon: 'none',
      duration: 2000
    });
    
    /*
    let that = this;
    wx.showActionSheet({
      itemList: ['修改头像'],
      success() {
        wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success(fileRes) {
            wx.showLoading({
              title: '上传中'
            })
            wx.uploadFile({
              filePath: fileRes.tempFilePaths[0],
              name: 'avatar',
              url: API.Upload.avatar,
              formData: {
                'type': app.globalData.usertype,
                'id': app.globalData.userinfo.id
              },
              success(res) {
                wx.hideLoading()
                let result = JSON.parse(res.data)//wx.uploadFile返回值是string
                wx.showToast({
                  title: result.message,
                  icon: 'none'
                })
                if(result.code == "SUCCESS") {
                  that.setData({
                    avatarUrl: result.data
                  })
                  app.globalData.userinfo.avatarUrl = result.data;
                  wx.setStorageSync('userinfo', app.globalData.userinfo);
                }
              },
              fail(res) {
                wx.hideLoading()
              }
            })
          },
        })
      }
    })
    */
  },
  
  tapOptionalFunction: function() {
    // 简化功能，不调用API
    wx.navigateTo({
      url: '/pages/queryHistory/queryHistory?customer_id=user123',
    });
    
    /*
    if(this.data.isLogin) {
      let type = app.globalData.usertype;
      if(type == "producer") {
        wx.navigateTo({
          url: '/pages/createCode/createCode',
        })
      } else if(type == "transporter") {
        if(this.data.location.address == "暂无定位信息") {
          wx.showModal({
            content: "小程序未获得位置授权，无法选择定位哦",
            confirmText: "去授权",
            success(res) {
              if(res.confirm) {
                wx.openSetting()
              }
            }
          })
          return
        } else if(this.data.location.address == "定位中") {
          util.showTips("正在定位，请稍候", 1500)
        }
        util.checkCameraPermission("小程序未获得相机授权，无法记录物流信息哦", '/pages/scanCode/scanCode?type=transporter&location='+this.data.location.address)
      } else if(type == "seller") {
        util.checkCameraPermission("小程序未获得相机授权，无法记录销售信息哦", '/pages/scanCode/scanCode?type=seller')
      } else if(type == "customer") {
        wx.navigateTo({
          url: '/pages/queryHistory/queryHistory?customer_id='+app.globalData.userinfo.id,
        })
      }
    }
    */
  },
  
  tapList: function(e) {
    let id = e.currentTarget.id;
    let that = this;
    
    if(id == 0) {
      wx.navigateTo({
        url: '/pages/updateInfo/updateInfo'
      });
    }
    else if(id == 1) {
      wx.navigateTo({
        url: '/pages/editPassword/editPassword'
      });
    }
    else if(id == 2) {
      wx.showModal({
        content: '确定要退出登录？',
        success(res) {
          if(res.confirm) {
            // 使用静态数据，不清除实际存储
            wx.showToast({
              title: '已退出登录（演示）',
              icon: 'success',
              duration: 2000
            });
            
            // 仍然保持登录状态，只是为了演示
            that.setData({
              isLogin: true
            });
            
            /*
            app.globalData.userinfo = [];
            app.globalData.usertype = "";
            app.globalData.isLogin = false;
            wx.removeStorageSync('userinfo');
            wx.removeStorageSync('usertype');
            wx.setStorageSync('isLogin', false);
            that.setData({
              isLogin: false,
              location: {
                address: "暂无定位信息",
              }
            });
            */
          }
        }
      });
    }
  },

  getLocation: function() {
    // 注释掉实际的地理位置获取功能，使用静态数据
    /*
    let that = this;
    that.setData({
      location: {
        address: "定位中"
      }
    });
    wx.getLocation({
      type: "gcj02",
      success(res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success(locationRes) {
            that.setData({
              location: {
                latitude: res.latitude,
                longitude: res.longitude,
                address: locationRes.result.formatted_addresses.recommend
              }
            }); 
          },
          fail(failRes) {
            that.setData({
              location: {
                latitude: res.latitude,
                longitude: res.longitude,
                address: "定位失败"
              }
            });
          }
        });
      }
    });
    */
    
    // 使用静态数据
    this.setData({
      location: {
        address: "北京市海淀区中关村南大街5号",
        latitude: 39.9761,
        longitude: 116.3282
      }
    });
  },

  chooseLocation: function() {
    // 注释掉实际的位置选择功能，使用静态数据
    wx.showToast({
      title: '静态演示模式，无法选择位置',
      icon: 'none',
      duration: 2000
    });
    
    /*
    let that = this;
    wx.getSetting({
      success(res) {
        if(res.authSetting['scope.userLocation'] == false) {
          wx.showModal({
            content: "小程序未获得位置授权，无法选择定位哦",
            confirmText: "去授权",
            success(res) {
              if(res.confirm) {
                wx.openSetting();
              }
            }
          });
        }
      }
    });
    wx.chooseLocation({
      latitude: that.data.location.latitude,
      longitude: that.data.location.longitude,
      success(res) {
        console.log(res);
        if(res.name != "") {
          that.setData({
            location: {
              latitude: res.latitude,
              longitude: res.longitude,
              address: res.name
            }
          });
        }
      }
    });
    */
  }
})
